import {
  ArgumentsHost,
  Catch,
  ExceptionFilter,
  HttpException,
} from '@nestjs/common';

@Catch(HttpException)
export class AppExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const request = ctx.getRequest();
    const status = exception.getStatus();

    // 获取详细的异常响应
    const exceptionResponse = exception.getResponse() as any;

    // 处理不同类型的异常信息
    let message = exception.message;
    message = exceptionResponse.message || message;
    response.status(status).json({
      code: -1,
      message: message || '服务器异常',
      timestamp: new Date().toISOString(),
      path: request.url,
      method: request.method, // 添加请求方法信息
    });
  }
}
